package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.LeaveStu;
import com.example.springboot.service.LeaveStuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/leave")
public class LeaveStuController {


    @Resource
    private LeaveStuService studentService;

    /**
     * 添加学生请假信息
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody LeaveStu leaveStu) {
        int i = studentService.addNewLeaveStu(leaveStu);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "添加失败");
        }

    }


    /**
     * 同意学生请假信息或者驳回学生请假信息
     */
    @GetMapping("/agreeOrRefuse")
    public Result<?> agreeAccount(@RequestParam(defaultValue = "") String username, @RequestParam(defaultValue = "") String isagree) {
        int i = studentService.agreeAccount(username,isagree);
        if (i == 1) {
            return Result.success(i);
        } else {
            return Result.error("-1", "修改失败");
        }
    }

    /**
     * 同意学生请假信息或者驳回学生请假信息
     */
    @GetMapping("/delete/{username}")
    public Result<?> delete(@PathVariable String username) {
        int i = studentService.delete(username);
        if (i == 1) {
            return Result.success(i);
        } else {
            return Result.error("-1", "修改失败");
        }
    }

    @GetMapping("/findByUserName/{username}")
    public Result<?> findByUserName(@PathVariable String username,
                                    @RequestParam(defaultValue = "0") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "") String search) {

        List<LeaveStu> leaveStuPage = studentService.findByUserName(username,pageNum, pageSize, search);
        if (leaveStuPage != null) {
            return Result.success(leaveStuPage);
        } else {
            return Result.error("-1", "查询失败");
        }
    }

    /**
     * 主页顶部：学生请假统计
     */
    @GetMapping("/leaveNum")
    public Result<?> leaveNum() {
        int num = studentService.leaveNum();
        if (num > 0) {
            return Result.success(num);
        } else {
            return Result.error("-1", "查询失败");
        }
    }


}
